local zishu = fk.CreateSkill{
  name = "wk_heg__zishu",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["wk_heg__zishu"] = "自书",
  [":wk_heg__zishu"] = "锁定技，①你的回合内，所有角色使用的非装备牌由你重新指定结算顺序，然后若此牌目标包含你，你摸一张牌；②其他角色的回合结束时，你将手牌中所有本回合获得的牌置入弃牌堆。",

  ["#wk_heg__zishu_choose"] = "自书：你可以重新指定此牌的结算顺序",
  ["@@wk_heg__zishu-inhand-turn"] = "自书",

  ["$wk_heg__zishu1"] = "慢着，让我来！",
  ["$wk_heg__zishu2"] = "身外之物，不要也罢！",
}

zishu:addEffect(fk.BeforeCardUseEffect,{
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(zishu.name) and player.room.current == player and #data.tos > 0 and data.card.type ~= Card.TypeEquip
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local id_tos = data.tos
    if #id_tos > 1 then
      local tos = room:askToChoosePlayers(player,{
        targets = id_tos,
        min_num = #id_tos,
        max_num = #id_tos,
        prompt = "#wk_heg__zishu_choose",
        skill_name = zishu.name,
        cancelable = false,
      })
      local new_tos = {}
      for _, p in ipairs(tos) do
        table.insert(new_tos, p)
      end
      data.tos = new_tos
    end
    local draw_card = false
    for _, p in ipairs(data.tos) do
      if p == player then
        draw_card = true
        break
      end
    end
    if draw_card then
      player:drawCards(1, zishu.name)
    end
  end,
})

zishu:addEffect(fk.TurnEnd,{
  anim_type ="special",
  can_trigger = function (self, event, target, player, data)
      return player:hasSkill(zishu.name) and target ~= player and table.find(player.player_cards[Player.Hand], function (id)
        return Fk:getCardById(id):getMark("@@wk_heg__zishu-inhand-turn") > 0
      end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = {}
    for _, id in ipairs(player:getCardIds(Player.Hand)) do
      if Fk:getCardById(id):getMark("@@wk_heg__zishu-inhand-turn") > 0 then
        table.insert(cards, id)
      end
    end
    if #cards > 0 then
    room:moveCards({
      from = player,
      ids = cards,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      skillName = zishu.name,
      proposer = player,
    })
    end
  end,
})

zishu:addEffect(fk.AfterCardsMove,{
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(zishu.name, true) and player.room.current ~= player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Player.Hand then
        for _, info in ipairs(move.moveInfo) do
          local id = info.cardId
          if room:getCardArea(id) == Card.PlayerHand and room:getCardOwner(id) == player then
            room:setCardMark(Fk:getCardById(id), "@@wk_heg__zishu-inhand-turn", 1)
          end
        end
      end
    end
  end,
})

return zishu